package cn.jinjimi.app.controller;

import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.Callable;

import bolts.Continuation;
import bolts.Task;
import cn.jinjimi.app.entity.AppConfig.HomeEntranceList;
import cn.jinjimi.app.entity.AppConfig.HomeCateList;
import cn.jinjimi.app.entity.AppConfig.HomeLimit;
import cn.jinjimi.app.entity.AppConfig.HomeNotices;
import cn.jinjimi.app.entity.Category;
import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.domain.data.dto.CommentList;
import cn.jinjimi.domain.entity.ProductCategory;
import cn.jinjimi.domain.entity.ProductList;
import cn.jinjimi.domain.data.dto.ConfigDTO;
import cn.jinjimi.domain.data.net.API;
import cn.jinjimi.domain.data.net.PageService;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class PageController {

    public static Task<ConfigDTO> requestPageConfig() {
        return Task.callInBackground(new Callable<ConfigDTO>() {
            @Override
            public ConfigDTO call() throws Exception {
                Call<ConfigDTO> req = API.create(PageService.class).getConfigs();
                Response<ConfigDTO> res = req.execute();
                if (res != null && res.isSuccessful()) {
                    return res.body();
                }
                return null;
            }
        }).continueWith(new Continuation<ConfigDTO, ConfigDTO>() {
            @Override
            public ConfigDTO then(Task<ConfigDTO> task) throws Exception {
                ConfigDTO config = task.getResult();
                if (task.isCompleted() && config != null) {
                    HomeEntranceList.save(config.getEntrances());
                    HomeCateList.save(config.getHome_cates());
                    HomeNotices.save(config.getNotices());
                    HomeLimit.save(config.getLimit());
                } else {
                    config = new ConfigDTO();
                    config.cates = HomeCateList.fetchHomeCateList();
                    config.entrances = HomeEntranceList.fetchHomeEntranceList();
                    config.notices = HomeNotices.fetchHomeNotices();
                    config.limit = HomeLimit.fetch();
                }
                return config;
            }
        });
    }

    public static Task<List<ProductList>> requestCate() {
        return Task.callInBackground(new Callable<List<ProductList>>() {
            @Override
            public List<ProductList> call() throws Exception {
                Call<ResponseBody> call = API.create(PageService.class).getCate();
                Response<ResponseBody> res = call.execute();
                if (res != null
                        && res.isSuccessful()
                        && res.body() != null) {
                    Type type = new TypeToken<List<ProductList>>(){}.getType();
                    return GsonUtils.fromJson(res.body().string(), type);
                }
                return null;
            }
        });
    }

    public static Task<CommentList> requestComments() {
        return Task.callInBackground(new Callable<CommentList>() {
            @Override
            public CommentList call() throws Exception {
                Response<ResponseBody> res = API.create(PageService.class).getComments().execute();
                if (res != null
                        && res.isSuccessful()
                        && res.body() != null) {
                    return GsonUtils.fromJson(res.body().string(), CommentList.class);
                }
                return null;
            }
        });
    }

    public static Task<ProductList> requestGoods() {
        return Task.callInBackground(new Callable<ProductList>() {
            @Override
            public ProductList call() throws Exception {
                Response<ProductList> res = API.create(PageService.class)
                                                .getGoods()
                                                .execute();
                if (res != null && res.isSuccessful()) {
                    return res.body();
                }
                return null;
            }
        });

    }

    public static Task<List<ProductCategory>> requestCategroy() {
        return Task.callInBackground(new Callable<List<ProductCategory>>() {
            @Override
            public List<ProductCategory> call() throws Exception {
                Response<ResponseBody> res =
                    API.create(PageService.class)
                        .queryCategroy()
                        .execute();
                if (res != null && res.isSuccessful()) {
                    ResponseBody body = res.body();
                    Type t = new TypeToken<List<ProductCategory>>() {}.getType();
                    return GsonUtils.fromJson(body.string(), t);
                }
                return null;
            }
        });
    }
}
